#include <iostream>
#include <bits/stdc++.h>
#include <climits> //INT_MAX
using namespace std;

int main() {
    int n = 0;
    string str1 = "";
    string str2 = "";
    vector<string> v_str;
    string tmp = "";
    cin >> n;
    cin >> str1 >> str2;
    while(n--){
        cin >> tmp;
        v_str.push_back(tmp);
    }
    int cur = 0;
    int index1 = -1,index2 = -1;
    int res = INT_MAX;
    while(cur < v_str.size()){
        if(index1 != -1 && index2 != -1){
            res = std::min(res,index1 > index2 ? index1-index2 : index2-index1);
        }
        if(v_str[cur] == str1){
            index1 = cur;
        }
        if(v_str[cur] == str2){
            index2 = cur;
        }
        ++cur;
    }
    if(index1 == -1 || index2 == -1){
        cout << -1;
    }
    else{
    cout << res;
    }
    
}